<?php

class UserController extends Controller
{

	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/topColumn';

    public $menuList;

    public $menuitemName;


	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
				'minLength'=>4,
				'maxLength'=>4,
                'testLimit'=>1,
            ),
		);
    }

    /**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('captcha','register'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('view','update'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView()
	{
		$this->render('view',array(
			'model'=>$this->loadModel(yii::app()->user->id),
		));
	}
	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate()
	{
		$model=$this->loadModel(yii::app()->user->id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['User']))
		{
            $model->oldpasswordrep = $model->password;
			$model->attributes=$_POST['User'];
            $model->verifyCode = $_POST['User']['verifyCode'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->user_id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}
	/**
	 * Register a new user.
	 * If registration is successful, login the user automatically and redirect to 
	 * the 'site/view' page.
	 * @author : xgqin <xgqin@guet.edu.cn>
	 * @copy date : 13/05/2014
	 */
	public function actionRegister()
	{
		$model=new User('create');

		$model->scenario='register';
		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
		{
			//$problemModel->validate();
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST['User']))
		{
			$model->user_name = $_POST['User']['user_name'];
			$model->nick_name = $_POST['User']['nick_name'];
			$model->url = $_POST['User']['url'];
			$model->password = $_POST['User']['password'];
			$model->email_address = $_POST['User']['email_address'];
			$model->school_name = $_POST['User']['school_name'];
			$model->last_login_ip = Yii::app()->getRequest()->getUserHostAddress();
			$model->verifyCode = $_POST['User']['verifyCode'];
			$model->password_repeat = $_POST['User']['password_repeat'];
			$model->tel = $_POST['User']['tel'];
			$model->job_number = $_POST['User']['job_number'];
			$model->college = $_POST['User']['college'];

			if($model->save())
			{
				$loginForm = new LoginForm();
				$loginForm->username = $model->user_name;
				$loginForm->password = $model->password;

				$loginForm->login();
				// Redirects the URL to the user profile.
				// We could redirect the url to a help page???
				// The help page could show some basic tips for the newly user?
				$this->redirect(array('view','id'=>$model->user_id));
			}
		}
		$this->render('register',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return User the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=User::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param User $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
